package com.zy.cat.controller.game.v1;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.entity.game.FruitCoinCountHistory;
import com.zy.cat.entity.game.FruitPrinceInfo;
import com.zy.cat.entity.game.FruitRetryCountHistory;
import com.zy.cat.entity.game.repository.FruitCoinCountHistoryRepository;
import com.zy.cat.entity.game.repository.FruitPrinceInfoRepository;
import com.zy.cat.entity.game.repository.FruitRetryCountHistoryRepository;
import com.zy.cat.entity.game.resp.CardPokerDrawHistoryResp;
import com.zy.cat.entity.game.resp.FruitCoinCountHistoryResp;
import com.zy.cat.entity.game.resp.FruitDrawHistoryResp;
import com.zy.cat.entity.game.resp.FruitInfoResp;
import com.zy.cat.entity.game.resp.FruitRetryCountHistoryResp;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatGameService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "9catGameFruit", tags = { "小游戏-水果机-开放API" }, description = "小游戏-水果机-开放接口")
@RestController

@RequestMapping("/v1/game/fruit")
public class CatGameFruitController {

	@Autowired
	CacheService cacheService;

	@Autowired
	CatGameService catGameService;

	@Autowired
	FruitRetryCountHistoryRepository fruitRetryCountHistoryRepository;

	@Autowired
	FruitCoinCountHistoryRepository fruitCoinCountHistoryRepository;
	
	@Autowired
	FruitPrinceInfoRepository fruitPrinceInfoRepository;
 

	@PostMapping("/info")
	@ApiOperation(value = "水果机信息", notes = "每次进入请求，返回游戏素材、进度等信息", consumes = "application/x-www-form-urlencoded", position = 1, response = FruitInfoResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_uuid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_utoken_uuid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getFruitInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<FruitInfoResp>> result = new Callable<RSPE<FruitInfoResp>>() {
			@Override
			public RSPE<FruitInfoResp> call() throws Exception {
				FruitInfoResp resultE = catGameService.getFruitInfo(rtype, pid);
				if (resultE == null) {
					return new RSPE<FruitInfoResp>(-3, "获取信息失败！请稍后重试！");
				}
				return new RSPE<FruitInfoResp>(catGameService.getFruitInfo(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/get/dcoin")
	@ApiOperation(value = "水果机获取代币", notes = "观看视频后获取、1000现金豆兑换5代币", consumes = "application/x-www-form-urlencoded", position = 2, response = FruitCoinCountHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "dtype", value = "获取代币方式(0.5次看视频、1.一次现金豆兑换、2.每日登录领取)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(utoken_uuid_dtype_uuid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "utoken_uuid_dtype_uuid_pid_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getFruitDcoin(Integer rtype, Integer dtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (dtype == null || dtype<0||dtype>2) {
			return new RSPE<Object>(-3, "获取代币方式参数错误！");
		}
		if (dtype == 0) {
			FruitCoinCountHistory fruit = fruitCoinCountHistoryRepository.findByPidAndCtimeAndDtype(pid,
					LocalDate.now().toString(), 205);
			if (fruit != null) {
				return new RSPE<Object>(-4, "看视频获取代币每天只能兑换5次!");
			}
		} else if (dtype == 1) {
			FruitCoinCountHistory fruit = fruitCoinCountHistoryRepository.findByPidAndCtimeAndDtype(pid,
					LocalDate.now().toString(), 1);
			if (fruit != null) {
				return new RSPE<Object>(-5, "代币每天只能兑换一次!");
			}
		}else if (dtype == 2) {
			FruitCoinCountHistory fruit = fruitCoinCountHistoryRepository.findByPidAndCtimeAndDtype(pid,
					LocalDate.now().toString(), 0);
			if (fruit != null) {
				return new RSPE<Object>(-6, "赠送代币每天只能领取一次!");
			}
		}
		Callable<RSPE<FruitCoinCountHistoryResp>> result = new Callable<RSPE<FruitCoinCountHistoryResp>>() {
			@Override
			public RSPE<FruitCoinCountHistoryResp> call() throws Exception {
				FruitCoinCountHistoryResp resultStr = catGameService.getFruitDcoin(rtype, dtype, pid);
				if (resultStr == null) {
					return new RSPE<FruitCoinCountHistoryResp>(0, "获取失败，请重试或联系客服人员！");
				}
				return new RSPE<FruitCoinCountHistoryResp>(resultStr);
			}
		};
		return result;
	}

	@PostMapping("/dcoin/double")
	@ApiOperation(value = "水果机进行抽奖翻倍", notes = "观看广告，进行奖励翻倍,仅支持抽奖奖励的现金豆和碎片，其他不支持翻倍！", consumes = "application/x-www-form-urlencoded", position = 3, response = FruitCoinCountHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "dtype", value = "中奖翻倍类型（1.1000现金豆兑换5代币（一次）、201~205.观看视频获得3~5代币（5次[201~205]））", example = "1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_pid_dtype_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_utoken_pid_dtype_rtype_uuid", MD5 = "token",checkSingle = true)
	@CheckDevPermission
	public Object getFruitDcoinDouble(Integer rtype,Integer dtype,  String pid , String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (dtype == null ||  (dtype != 1 &&(dtype<201||dtype>205) )) {
			return new RSPE<Object>(-3, "获取代币方式参数错误！");
		}
		
		FruitCoinCountHistory fruit = fruitCoinCountHistoryRepository.findByPidAndCtimeAndDtype(pid,
				LocalDate.now().toString(), dtype);
		if (fruit == null) {
			return new RSPE<Object>(-4, "翻倍不存在!");
		}
		if (fruit.getFtype()==1) {
			return new RSPE<Object>(-5, "已经翻倍，请不要重复请求！");
		}
		Callable<RSPE<FruitCoinCountHistoryResp>> result = new Callable<RSPE<FruitCoinCountHistoryResp>>() {
			@Override
			public RSPE<FruitCoinCountHistoryResp> call() throws Exception {
				return new RSPE<FruitCoinCountHistoryResp>(catGameService.getFruitDcoinDouble(rtype,dtype,pid));
			}
		};
		return result;
	}
	
	@PostMapping("/retry")
	@ApiOperation(value = "水果机游戏进行复活", notes = "每天5次复活机会！", consumes = "application/x-www-form-urlencoded", position =4, response = FruitRetryCountHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_pid_rtype_utoken_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_pid_pid_rtype_utoken_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getFruitRetry(Integer rtype,  String pid , String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		FruitRetryCountHistory retry_1 = fruitRetryCountHistoryRepository.findByPidAndCtimeAndDtype(pid,
				LocalDate.now().toString(), 1);
		FruitRetryCountHistory retry_0 = fruitRetryCountHistoryRepository.findByPidAndCtimeAndDtype(pid,
				LocalDate.now().toString(), -1);
		if (retry_1 == null) {
			return new RSPE<Object>(-2, "复活次数不存在!");
		}
		if(retry_0==null) {
			if (retry_1.getDcount()<1) {
				return new RSPE<Object>(-5, "复活次数已用完！请明日重试！",new FruitRetryCountHistoryResp(0, LocalDate.now().plusDays(1).toString()+" 00:00:00"));
			}
		}else {
			if (retry_1.getDcount()-retry_0.getDcount()<1) {
				return new RSPE<Object>(-5, "复活次数已用完！请明日重试！",new FruitRetryCountHistoryResp(0, LocalDate.now().plusDays(1).toString()+" 00:00:00"));
			}
		}
		
		Callable<RSPE<FruitRetryCountHistoryResp>> result = new Callable<RSPE<FruitRetryCountHistoryResp>>() {
			@Override
			public RSPE<FruitRetryCountHistoryResp> call() throws Exception {
				return new RSPE<FruitRetryCountHistoryResp>(catGameService.getFruitRetry(rtype,pid));
			}
		};
		return result;
	}
	
	
	@PostMapping("/draw")
	@ApiOperation(value = "水果机游戏结果统计", notes = "游戏成功或者失败！", consumes = "application/x-www-form-urlencoded", position = 3, response = FruitDrawHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "fpid", value = "奖品ID", example = "1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "gtype", value = "关卡，1、2、3", example = "1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "gstatus", value = "闯关状态：0.闯关失败、1.闯关才成功", example = "1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_fpid_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_fpid_pid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getFruitDraw(Integer rtype,String pid ,String fpid ,Integer gtype,Integer gstatus, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (gtype == null || gtype<1 || gtype>3) {
			return new RSPE<Object>(-3, "关卡参数错误！");
		}
		if (gstatus == null || gstatus<0 || gstatus>1) {
			return new RSPE<Object>(-4, "闯关状态错误！");
		}
 
		FruitPrinceInfo fruitPrince=fruitPrinceInfoRepository.findByFpidAndPstatus(fpid,1);
		if (fruitPrince==null) {
			return new RSPE<Object>(-5, "奖品不存在");
		}
		Callable<RSPE<FruitDrawHistoryResp>> result = new Callable<RSPE<FruitDrawHistoryResp>>() {
			@Override
			public RSPE<FruitDrawHistoryResp> call() throws Exception {
				return new RSPE<FruitDrawHistoryResp>(catGameService.getFruitDeaw(rtype,pid,fpid,gtype,gstatus,fruitPrince));
			}
		};
		return result;
	}
	
	@PostMapping("/scroll/bar/list")
	@ApiOperation(value = "水果机滚动条展示内容", notes = "水果机滚动条展示内容 ！", consumes = "application/x-www-form-urlencoded", position = 4, response = String.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getFruitScrollBarList(Integer rtype, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		
		Callable<RSPE<List<String>>> result = new Callable<RSPE<List<String>>>() {
			@Override
			public RSPE<List<String>> call() throws Exception {
				return new RSPE<List<String>>(catGameService.getFruitScrollBarList(rtype));
			}
		};
		return result;
	}
}
